/**
 *
 */
package com.cbwl.eoas.cms.web.controller.user;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cbwl.eoas.cms.api.user.IDataSysUserService;
import com.cbwl.eoas.cms.model.DataSysUser;
import com.cbwl.eoas.common.framework.vo.PageRequest;
import com.cbwl.eoas.common.util.UuidGenerator;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.web.Result;
import com.github.pagehelper.PageInfo;

/**
 * @author yyb
 *
 */
@Controller
@RequestMapping("user")
public class UserController
{
	

	@Autowired
	private IDataSysUserService dataSysUserService;

	private static Logger logger = LoggerFactory.getLogger(UserController.class);

	
	
	
	
	
	
	@RequestMapping("userAllList")
	@ResponseBody
	public Result userList(DataSysUser user)
	{
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		try
		{

			// 分页查询用户信息
			List<DataSysUser> list = dataSysUserService.queryObjectList(user);

			result.setData(list);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.error("查询失败", e);
			result.setStatus(Result.Status.ERROR);
			result.setMessage("查询失败");
			return result;
		}

		return result;
	}
	
	
	
	/**
	 * 获取用户列表
	 *
	 * @param roleid
	 * @param name
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("userList")
	@ResponseBody
	public Result userList(DataSysUser bean, int page,String draw, int rows,String param)
	{
		Result result = new Result(Result.Status.OK, "查询成功", "200");
		try
		{
				page = (page/rows)+1;
				PageRequest pageRequest =  new PageRequest();
			// 分页查询用户信息
			PageInfo<?> userPageInfo = dataSysUserService.queryPageList(bean, pageRequest);

			result.setData(userPageInfo);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			logger.error("查询失败", e);
			result.setStatus(Result.Status.ERROR);
			result.setMessage("查询失败");
			return result;
		}

		return result;
	}
	
	
	@RequestMapping(value="initUserManager")
	public String initUserManager()
	{
		return "initUserManager";
	}
	
	
	@RequestMapping(value="user-add")
	public String userAdd(HttpServletRequest request,DataSysUser bean){
		DataSysUser databean = dataSysUserService.getObjectById(bean);
		request.setAttribute("bean", databean);
		return "userAdd";
	}
	
	
	@RequestMapping(value="deleteUserManager")
	@ResponseBody
	public Object deleteUserManager(DataSysUser bean) throws Exception{
		return dataSysUserService.deleteObject(bean);
	}
	
	@RequestMapping(value="updateUserManager")
	@ResponseBody
	public Object updateUserManager(DataSysUser bean) throws Exception{
		
		DataSysUser databean = dataSysUserService.getObjectById(bean);
		bean.setVersion(databean.getVersion());
		bean.setUpdateDate(new Date());
		bean.setUpdateUser("sysadmin");
		return dataSysUserService.updateObject(bean);
	}
	
	@RequestMapping(value="saveUserManager")
	@ResponseBody
	public Object saveUserManager(DataSysUser bean) throws Exception{
		
		
		bean.setUuid(UuidGenerator.getUuid());
		bean.setCreateDate(new Date());
		bean.setCreateUser("sysadmin");
		bean.setStatus("1");
		bean.setVersion("1");
		//默认小件员
		bean.setUserType(DicDetailConstant.user_type_2);
		return dataSysUserService.saveObject(bean);
	}
	
	
	
	@RequestMapping(value="getUserManager")
	@ResponseBody
	public Object getUserManager(DataSysUser bean) throws Exception{
		return dataSysUserService.getObjectById(bean);
	}
	
	
	


}
